from django.contrib.auth import authenticate,login
from django.utils.translation import ugettext_lazy as _
from django.core.context_processors import csrf
from django.http import Http404,HttpResponse,HttpResponseRedirect

def login_page(request):
    result={}
    result.update(csrf(request))
    result['errors']=[]
    p=request.POST
    if (not "username" in p) or (not "password" in p):
        return render_to_response('login.html',result)
    if not p['username'] or not p['password']:
        result['errors'].append("username/password is empty")
        return render_to_response('login.html',result)
    cuser=authenticate(username=p['username'],password=p['password'])
    if cuser is not None:
        if cuser.is_active:
            login(request,cuser)
            return HttpResponseRedirect(request.GET['next'] or "/")
        else:
            result['errors'].append('user is not activate')
            return render_to_response('login.html',result)
    else:
        result['errors'].append('username/password is invalid')
        return render_to_response('login.html',result)
    
def logout_page(request):
    logout(request)
    return HttpResponseRedirect(request.GET['next'] or "/")

def register_function(request):
    pass
